import {
  oldDrawQrcode,
  drawMyQrcode,
  subscribeQrcodec,
} from "../../utils/util";
import * as QR from "../../static/js/qrcode";

Component({
  /**
   * 组件的属性列表
   */
  data: {
    screenBrightness: "",
  },
  properties: {
    // 名字标识
    flagname: {
      type: String,
      value: "",
    },
    phone: {
      type: String,
      value: "",
    },
    houseName: {
      type: String,
      value: "",
    },
    commid: {
      type: String,
      value: "",
    },
    visitorPhone: {
      type: String,
      value: "",
    },
    qrCodeVersion: {
      type: String,
      value: "",
    },
    code: {
      type: Array,
      value: [],
    },
    isVisitor: {
      type: Boolean,
      value: false,
    },
    unitnos: {
      type: Array,
      value: [],
    },
    communityId: {
      type: String,
      value: "",
    },
    interviewPhone: {
      type: String,
      value: "",
    },
    openPhone: {
      type: String,
      value: "",
    },
    time: {
      type: String,
      value: "",
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    imageUrl: "",
  },
  lifetimes: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {},
  onUnload: function () {},
  ready: function () {
    // 从缓存中获取unitnos
    // let unitnos=wx.getStorageSync("unitnos");
    // let src = drawMyQrcode(this.data.phone,this.data.houseName,this.data.commid,this.data.visitorPhone,unitnos)
    // this.setData({
    //   imageUrl:src
    // })
    this.drawQrcode();
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 小程序二维码生成函数（包括老版本和新版本）
    drawQrcode() {
      //扫码开门生成二维码
      if (this.data.flagname == "doorName") {
        this.doorQrcode();
        return false;
      }

      let src;
      if (this.data.isVisitor) {
        let qrCodeVersion = this.data.qrCodeVersion;
        if (qrCodeVersion === "V1") {
          src = oldDrawQrcode(
            this.data.phone,
            this.data.houseName,
            this.data.commid,
            this.data.visitorPhone
          );
        } else {
          // 从缓存中获取unitno
          let code = this.data.code;
          src = drawMyQrcode(
            this.data.phone,
            this.data.houseName,
            this.data.commid,
            this.data.visitorPhone,
            code
          );
        }
      } else {
        let qrCodeVersion = wx.getStorageSync("qrCodeVersion");
        if (qrCodeVersion === "V1") {
          src = oldDrawQrcode(
            this.data.phone,
            this.data.houseName,
            this.data.commid,
            this.data.visitorPhone
          );
        } else {
          if (this.data.interviewPhone) {
            // 我的邀约
            src = subscribeQrcodec(
              this.data.time,
              this.data.interviewPhone,
              this.data.communityId,
              this.data.openPhone,
              this.data.code
            );
          } else {
            // 从缓存中获取unitno
            let code = wx.getStorageSync("code");
            src = drawMyQrcode(
              this.data.phone,
              this.data.houseName,
              this.data.commid,
              this.data.visitorPhone,
              code
            );
          }
        }
      }

      this.setData({
        imageUrl: src,
      });
    },
    doorQrcode() {
      let obj = Object.assign(
        {},
        {
          openPhone: this.data.phone,
          communityId: this.data.commid,
          time: this.data.time,
          code: this.data.code,
          interviewPhone: this.data.visitorPhone,
        }
      );
      let message = JSON.stringify(obj);
      // 不加密
      let tex = message;
      let text = "&" + tex + "#";
      var imgData = QR.drawImg(text, {
        typeNumber: 4,
        errorCorrectLevel: "H",
        size: 500,
      });
      this.setData({
        imageUrl: imgData,
      });
    },
    refresh() {
      // let imgData = drawMyQrcode(this.data.phone,this.data.houseName,this.data.commid,this.data.visitorPhone,unitnos)
      // this.setData({
      //   imageUrl:imgData
      // })
      this.drawQrcode();
    },
  },
});
